package com.agentlab.imitation.web.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.agentlab.imitation.bean.User;
import com.agentlab.imitation.bean.UserWish;
import com.agentlab.imitation.common.CommonConstant;
import com.agentlab.imitation.service.UserWishService;
import com.agentlab.imitation.util.DateTimeUtils;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("unused")
@Controller 
public class ImitationAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource UserWishService userWishService;
	
	public String begin(){
		HttpServletRequest req = ServletActionContext.getRequest();
		String from = req.getParameter("from");
		if("again".equals(from))return "begin";
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute(CommonConstant.USER_SESSION);
		if(userWishService.exsit(user.getUserid())){
			session.setAttribute(CommonConstant.USER_WISH_SESSION, userWishService.find(user.getUserid()));
			return "simpleCard";
		}
		return "begin";
	}
	
	/**
	 * 进入志愿填报单页面
	 */
	public String  submit(){
		HttpServletRequest req = ServletActionContext.getRequest();
		String xinli = req.getParameter("xinli");
		String kl = req.getParameter("wl");
		String prescore = req.getParameter("prescore");
		String mode = req.getParameter("mode");
		String aihao = "";
		String diyu = "";
		String shoufei = "";
		String[] aihaos = req.getParameterValues("aihao[]");
		if(aihaos != null){
			for(String str:aihaos){
				aihao += str+"|";
			}
			aihao = aihao.substring(0, aihao.length() - 1);
		}
		String[] diyus = req.getParameterValues("diyu");
		if(diyus != null){
			for(String str:diyus){
				diyu += str+"|";
			}
			diyu = diyu.substring(0, diyu.length() - 1);
		}
		String[] shoufeis = req.getParameterValues("shoufei[]");
		if(shoufeis != null){
			for(String str:shoufeis){
				shoufei += str+"|";
			}
			shoufei = shoufei.substring(0, shoufei.length() - 1);
		}
		
		System.out.println("prescore=" + prescore);
		System.out.println("xinli=" + xinli);
		System.out.println("kl=" + kl);
		
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute(CommonConstant.USER_SESSION);
		UserWish userWish = new UserWish(user.getUserid(), user.getRealname(), aihao, diyu, shoufei, xinli, mode, prescore, kl, DateTimeUtils.getDateTimeStamp(), DateTimeUtils.getDateTimeStamp());
		if(!userWishService.exsit(user.getUserid()))userWishService.save(userWish);
		else userWishService.update(userWish);
		session.removeAttribute(CommonConstant.USER_WISH_SESSION);
		session.setAttribute(CommonConstant.USER_WISH_SESSION, userWish);
		return "submit";
	}
	
}
